/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application.validator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author aa6345
 */
public class SearchFormValidator {

    private ArrayList<String> inputValidationErrorList = new ArrayList<String>();

    public ArrayList<String> getInputValidationErrorList() {
        return inputValidationErrorList;
    }

    public void setInputValidationErrorList(ArrayList<String> inputValidationErrorList) {
        this.inputValidationErrorList = inputValidationErrorList;
    }

    public void addToInputValidationErrorList(String errorString) {
        this.inputValidationErrorList.add(errorString);
    }

    public String validateSearchTerm(String searchTerm) {
        if(searchTerm != null) {
            if (this.validateStringInput(searchTerm)) {
                searchTerm = this.modifyStringInput(searchTerm);
            } else {
                searchTerm = "Input Validation Issue - validateSearchTerm";
                this.addToInputValidationErrorList(searchTerm);
            }
        }
        return searchTerm;
    }

    /*
     * Only 'None', 'AND' or 'NOT' accepted. 'None' is default.
     */
    public String validateOperator(String operatorValue) {

        if (operatorValue != null) {
            if (operatorValue.trim().toLowerCase().equals("and")) {
                operatorValue = operatorValue.trim().toUpperCase();
            } else if (operatorValue.trim().toLowerCase().equals("not")) {
                operatorValue = operatorValue.trim().toUpperCase();
            } else {
                operatorValue = "None";
            }
        } else {
            operatorValue = "None";
        }

        return operatorValue;
    }

    /*
     * list of strings accepted
     */
    public String validateSynonyms(List operatorValue) {

        String validatedSynonyms = "";
        String validatedSynonymSeperator = ";;";

        if (operatorValue != null) {
            Iterator<String> iterator = operatorValue.iterator();

            while (iterator.hasNext()) {
                String validatedSynonym = validateSearchTerm(iterator.next());
                if (!validatedSynonym.equals("Input Validation Issue")) {
                    validatedSynonyms += validatedSynonym + validatedSynonymSeperator;
                }
                //System.out.println( iterator.next() );
            }
        }
        return validatedSynonyms;
    }

    /*
     * Only 'term' or 'enity' accepted. Term default
     */
    public String validateSearchType(String searchType) {
        if (searchType != null) {
            if (searchType.trim().toLowerCase().equals("term")) {
                searchType = searchType.trim().toUpperCase();
            } else if (searchType.trim().toLowerCase().equals("entity")) {
                searchType = searchType.trim().toUpperCase();
            } else {
                searchType = "term";
            }
        } else {
            searchType = "term";
        }
        return searchType;
    }

    /*@descrition: Checks if string empty, blank space, contains 'select ',
     * 'select*', 'delete ', 'update ', 'insert ', ';', '='
     */
    private Boolean validateStringInput(String inputString) {
        Boolean isValid = false;
        
        if (!inputString.isEmpty()) {
            //search by entityID will fail here for entity IDs 1 to 9
            //if (inputString.trim().length() > 1) {
                
                if (!inputString.toLowerCase().contains("select ") || !inputString.toLowerCase().contains("select*")) {
                    
                    if (!inputString.toLowerCase().contains("insert")) {
                        
                        if (!inputString.toLowerCase().contains("update")) {
                            
                            if (!inputString.toLowerCase().contains(" or ")) {
                                
                                if (!inputString.toLowerCase().contains(" and ")) {
                                    
                                    if (!inputString.toLowerCase().contains(" where ")) {
                                        
                                        if (!inputString.trim().contains(";")) {
                                            
                                            if (!inputString.trim().contains("=")) {
                                                
                                                if (!inputString.trim().contains("+")) {

                                                    //some entity names have -
                                                    //if (!inputString.trim().contains("-")) {
                                                        
                                                        if (!inputString.trim().contains("%")) {

                                                            if (!inputString.trim().contains("*")) {
                                                                isValid = true;
                                                            }
                                                        }
                                                    //}
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            //}
        }

        if (isValid == false) {
            this.addToInputValidationErrorList("Input Validation Issue - validateStringInput, "+inputString);
        }
        return isValid;
    }

    /*
     * @description: excapes caracters that could break the query. ',"
     */
    private String modifyStringInput(String inputString) {
        if (inputString.contains("'")) {
            inputString = inputString.replace("'", "\\'");
        }
        if (inputString.contains("\"")) {
            inputString = inputString.replace("\"", "\"");
        }
        inputString = inputString.trim();
        return inputString;
    }
}
